<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import axios from "axios";
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  data() {
    return {
      allData: null,
      chartInstance: null,
      mapData: {} //所获取的省份地图矢量数据
    };
  },
  created() {
    this.$socket.registerCallBack("mapData", this.getData);
  },
  computed: {
    ...mapState(["theme"])
  },
  watch: {
    theme() {
      this.chartInstance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    }
  },
  mounted() {
    this.initChart();
    this.$socket.send({
      action: "getData",
      socketType: "mapData",
      chartName: "map",
      value: ""
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("mapData");
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
      //获取数据，不是从后端获取，而是直接从public/static/map获取
      const res = await axios.get(
        "http://localhost:8999/static/map/china.json"
      );
      this.$echarts.registerMap("china", res.data);

      const initOption = {
        title: {
          text: "▎ 商家分布",
          left: 20,
          top: 20
        },
        geo: {
          type: "map",
          map: "china",
          top: "5%",
          bottom: "5%",
          itemStyle: {
            areaColor: "#2E72BF",
            borderColor: "#333"
          }
        },
        legend: {
          left: "5%",
          bottom: "5%",
          orient: "vertical"
        }
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("click", async arg => {
        //arg.name得到所点击的省份，需要中文转为拼音
        const provinceInfo = getProvinceMapInfo(arg.name);

        //需要获取这个省份的地图矢量数据，判断是否在mapData中存在
        if (!this.mapData[provinceInfo.key]) {
          const res = await axios.get(
            "http://localhost:8999" + provinceInfo.path
          );
          this.mapData[provinceInfo.key] = res.data;
          this.$echarts.registerMap(provinceInfo.key, res.data);
          const changeOption = {
            geo: {
              map: provinceInfo.key
            }
          };
          this.chartInstance.setOption(changeOption);
        }
      });
    },
    async getData() {
      const { data: res } = await this.$http.get("map");
      this.allData = res;
      this.updateChart();
    },
    updateChart() {
      //处理图表的数据
      //图例的数据
      const legendArr = this.allData.map(item => {
        return item.name;
      });
      const seriesArr = this.allData.map(item => {
        return {
          type: "effectScatter",
          name: item.name,
          data: item.children,
          rippleEffect: {
            scale: 5,
            brushType: "stroke"
          },
          coordinateSystem: "geo"
        };
      });
      const dataOption = {
        legend: {
          data: legendArr
        },
        series: seriesArr
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    revertMap() {
      const revertOption = {
        geo: {
          map: "china"
        }
      };
      this.chartInstance.setOption(revertOption);
    }
  }
};
</script>
<style lang="less" scoped></style>
